package com.xcm.controller;

import com.google.common.collect.Lists;
import com.xcm.export.entity.SingleProgramDetailReportData;
import com.xcm.export.util.SimpleExportUtil;
import com.xcm.model.vo.SingleDeviceReportVO;
import com.xcm.model.vo.WholeDeviceReportVO;
import com.xcm.model.vo.WholeProgramReportVO;
import com.xcm.service.DeviceInfoService;
import com.xcm.service.ProgramJsonService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping(value = "/export")
public class ExportController {

    private static Logger logger = LoggerFactory.getLogger(ExportController.class);
    @Autowired
    private ProgramJsonService programJsonService;
    @Autowired
	private DeviceInfoService deviceInfoService;
    /**
     * 导出单个报表详细报表
     */
    @RequestMapping(value = "/exportSingleProgramDetailReport")
    public void exportSingleProgramDetailReport(String receiveStartTime, String receiveEndTime, Long programJsonId, HttpServletResponse response) {
        try (OutputStream out = response.getOutputStream()){
            List<SingleProgramDetailReportData> exportList = null;
            if (StringUtils.isBlank(receiveStartTime) && StringUtils.isBlank(receiveEndTime) && programJsonId == null) {
                exportList = Lists.newArrayList();
            } else {
                exportList = programJsonService.listExportSingleProgramDetailReport(receiveStartTime, receiveEndTime, programJsonId);
            }
            //设置输出类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("单个广告详细报表列表.xls").getBytes(), "ISO-8859-1"));
            SimpleExportUtil.exportToExcel(exportList, "单个广告详细报表列表").write(out);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ExportController exportSingleProgramDetailReport 导出单个报表详细报表失败：" + e.getMessage());
        }
    }
    
    /**
     * 导出单个终端详细报表
     */
    @RequestMapping(value = "/exportSingleDeviceDetailReport")
    public void exportSingleDeviceDetailReport(String startTime, String endTime, String deviceNum,String mac, HttpServletResponse response) {
        try (OutputStream out = response.getOutputStream()){
            List<SingleDeviceReportVO> exportList = deviceInfoService.listExportSingleDeviceDetailReport(startTime, endTime, deviceNum, mac);
            //设置输出类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("单个终端详细报表列表.xls").getBytes(), "ISO-8859-1"));
            SimpleExportUtil.exportToExcel(exportList, "单个终端详细报表列表").write(out);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ExportController exportSingleDeviceDetailReport 导出单个终端详细报表失败：" + e.getMessage());
        }
    }


    /**
     * 导出整体终端报表
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param deviceNum 机器码
     * @param mac mac地址
     * @param province 省
     * @param city 市
     * @param area 区/县
     * @param groupCode 分组码
     * @param response 响应
     */
    @RequestMapping(value = "/exportWholeDeviceReport")
    public void exportWholeDeviceReport(String startTime, String endTime, String deviceNum, String mac,
                                        String province, String city, String area, String groupCode, HttpServletResponse response) {
        try (OutputStream out = response.getOutputStream()){
            List<WholeDeviceReportVO> wholeDeviceReportVOList = deviceInfoService.listWholeDeviceReport(startTime, endTime,
                    deviceNum, mac, province, city, area, groupCode);
            //设置输出类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("整体终端报表.xls").getBytes(), "ISO-8859-1"));
            SimpleExportUtil.exportToExcel(wholeDeviceReportVOList, "整体终端报表").write(out);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ExportController exportWholeDeviceReport 导出整体终端报表失败：" + e.getMessage());
        }
    }

    /**
     * 导出整体广告报表
     * @param timeType 1:接收时间 2:完成时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param publishWay 发布方式 1：定时发布(当前只有定时发布) 2:预约发布
     * @param programJsonId 发布Id
     * @param response
     */
    @RequestMapping(value = "/exportWholeProgramReport")
    public void exportWholeProgramReport(Integer timeType, String startTime, String endTime, Integer publishWay, Long programJsonId, HttpServletResponse response) {
        try(OutputStream out = response.getOutputStream()) {
            List<WholeProgramReportVO> wholeProgramReportVOList = programJsonService.listWholeProgramReport(timeType, startTime, endTime, publishWay, programJsonId);
            if(null == wholeProgramReportVOList) {
                return ;
            }
            //设置输出类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("整体广告报表.xls").getBytes(), "ISO-8859-1"));
            SimpleExportUtil.exportToExcel(wholeProgramReportVOList,"整体广告报表").write(out);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ExportController exportWholeDeviceReport 导出整体广告报表失败：" + e.getMessage());
        }
    }
}
